﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Freshome.m.vws
{
    /// <summary>
    /// Recipe.xaml 的交互逻辑
    /// </summary>
    public partial class Recipe : Window
    {
        private const string _ep = "wsHttp_IMultint";
        private readonly fox.Userole _ur = null;
        private readonly fox.Helproxy<Rmt.IMultint> _hp;
        private short _id;

        public Recipe(short mid, fox.Userole ur)
        {
            InitializeComponent();
            this._id = mid;
            this._ur = ur;
            this._hp = new fox.Helproxy<Rmt.IMultint>(ur);
            topanel.Loaded += new RoutedEventHandler(topanel_Loaded);
            this._ld.DoWork += new DoWorkEventHandler(Load_DoWork);
            this._ld.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Load_WorkerCompleted);
        }
        private BackgroundWorker _ld = new BackgroundWorker();

        private mds.Recipe _rp = null;
        public bool Readonly
        {
            get { return !this._ur.IsJunior; }
        }
        public bool Enabled
        {
            get { return this._ur.IsJunior; }
        }
        void Load_DoWork(object sender, DoWorkEventArgs e)
        {
            _hp.Asyncall(cln =>
            {
                e.Result = cln.GetRecipe(this._id, true);
            }, new Rmt.MultintClient(_ep));
        }
        // The Recipe Loading finished
        void Load_WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null) MessageBox.Show(e.Error.Message, "数据服务");
            else
            {
                var rt = e.Result as Rmt.Recipe;
                if (rt != null)
                {
                    if (this._rp == null) this._rp = new mds.Recipe(rt);
                    else this._rp.Refresh(rt);
                    if (rt.Steps != null)
                    {
                        fox.Kitools.Refresh(rt.Steps, _rp.Steps);
                    }
                    if (rt.Minors != null)
                    {
                        fox.Kitools.Refresh(rt.Minors, _rp.Minors);
                    }
                    if (rt.Majors != null)
                    {
                        fox.Kitools.Refresh(rt.Majors, _rp.Majors);
                    }
                }
            }
            topanel.DataContext = this._rp;
            this.dgi.IsReadOnly = !this._ur.IsJunior;
            this.dgj.IsReadOnly = !this._ur.IsJunior;
        }
        void topanel_Loaded(object sender, RoutedEventArgs e)
        {
            if (!_ld.IsBusy)
            {
                _ld.RunWorkerAsync();
                this.dgi.IsReadOnly = true;
                this.dgj.IsReadOnly = true;
            }
        }
        // Save updated Majorecipes or Minorecipes
        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (_rp != null)
            {
                e.CanExecute = _rp.Majors.Any(t => t.Updated) || _rp.Minors.Any(t => t.Updated);
            }
            else e.CanExecute = false;
        }
        private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Dictionary<short, string> rts = null;
            var mis = from m in _rp.Minors where m.Updated select m.Raw;
            foreach (var mi in mis)
            {
                mi.Stamp = string.Format("{0}+{1}", mi.Imx, mi.Rmx);
            }
            if (mis.Count() > 0)
            {
                _hp.Syncall(cln =>
                {
                    rts = cln.SaveMinorecipes(mis.ToArray());
                }, new Rmt.MultintClient(_ep));
                if (rts != null)
                {
                    fox.Kitools.Replace<mds.Minorecipe, Rmt.Minorecipe>(rts, _rp.Minors.Where(t => t.Updated), "菜谱佐料");
                }
            }
            var mjs = from m in _rp.Majors where m.Updated select m.Raw;
            foreach (var mj in mjs)
            {
                mj.Stamp = string.Format("{0}+{1}", mj.Jmx, mj.Rmx);
            }
            if (mjs.Count() > 0)
            {
                _hp.Syncall(cln =>
                {
                    rts = cln.SaveMajorecipes(mjs.ToArray());
                }, new Rmt.MultintClient(_ep));
                if (rts != null)
                {
                    fox.Kitools.Replace<mds.Majorecipe, Rmt.Majorecipe>(rts, _rp.Majors.Where(t => t.Updated), "菜谱主料");
                }
            }
        }
    }
}
